package org.springblade.modules.admin.service.feishu.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springblade.common.enums.FeiShuDocumentEnum;
import org.springblade.modules.admin.dto.feishu.FeiShuCreateBatchDTO;
import org.springblade.modules.admin.dto.feishu.FeiShuFieldsDTO;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.service.feishu.IFeiShuDocumentService;
import org.springblade.modules.admin.service.feishu.IFeiShuElevatorService;
import org.springblade.modules.admin.vo.feishu.BuildingElevatorFeiShuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: Sharry
 * @createTime: 2024-02-03 9:54
 * @version: Version-1.0
 */
@Service
public class FeiShuElevatorServiceImpl implements IFeiShuElevatorService {

	@Autowired
	private IFeiShuDocumentService feiShuDocumentService;

	@Autowired
	private ElevatorService elevatorService;

	@Override
	public void updateProjectElevatorInfo() {
		// 1. 获取目标文件夹Token、文档ID、目标数据
		// 1.1 目标文件夹Token、文档ID
		String appToken = FeiShuDocumentEnum.BUILDING_ELEVATOR_UPDATE.getAppToken();
		String tableId = FeiShuDocumentEnum.BUILDING_ELEVATOR_UPDATE.getTableId();

		// 1.2 需要更新的数据列表
		List<BuildingElevatorFeiShuVO> buildingElevatorFeiShuVOList = elevatorService.queryBuildingElevatorVOList();
		// 2. 数据处理；数据封装
		FeiShuCreateBatchDTO feiShuCreateBatchDTO = new FeiShuCreateBatchDTO();
		List<FeiShuFieldsDTO> records = new ArrayList<>();
		for (BuildingElevatorFeiShuVO b : buildingElevatorFeiShuVOList) {
			String areaMap = b.getLongitude() + "," + b.getLatitude();
			Integer maintainStatus = b.getMaintainStatus();
			switch (maintainStatus) {
				case -1:
					b.setMaintainStatusDesc("无状态");
					break;
				case 1:
					b.setMaintainStatusDesc("脱保");
					break;
				case 2:
					b.setMaintainStatusDesc("在保（合同期内）");
					break;
				case 3:
					b.setMaintainStatusDesc("在保（合同期外）");
					break;
				case 4:
					b.setMaintainStatusDesc("在保（拟定保养）");
					break;
				default:
					b.setMaintainStatusDesc("数据缺失");
					break;
			}

			b.setUseTypeDesc(new Integer(0).equals(b.getUseType()) ? "年审" : "非年审");

			FeiShuFieldsDTO element = new FeiShuFieldsDTO();
			Map<String,Object> elementMap = new HashMap<>(10);
			elementMap.put("elevator_address", b.getElevatorAddress());
			elementMap.put("elevator_num", b.getElevatorNum());
			elementMap.put("equipment_register_number", b.getEquipmentRegisterNumber());
			elementMap.put("name", b.getName());
			elementMap.put("private_num", b.getPrivateNum());
			elementMap.put("register_number", b.getRegisterNumber());
			elementMap.put("梯无忧_定位（地图地址文本）", areaMap);
			elementMap.put("在保状态", b.getMaintainStatusDesc());
			elementMap.put("电梯年检类型",b.getUseTypeDesc());
			element.setFields(elementMap);
			records.add(element);
		}
		// records 去重
		Set<FeiShuFieldsDTO> set = new HashSet<>(records);
		// 清空 records, 将 set 中的数据添加到 records 中
		records.clear();
		records.addAll(set);
		feiShuCreateBatchDTO.setRecords(records);
		String result = feiShuDocumentService.commandCompareUpdateBatch(appToken, tableId,feiShuCreateBatchDTO,"elevator_num");
		// 5. 发送消息, 通知到群
		List<JSONObject> targetGroupObjects = feiShuDocumentService.commandQueryGroupsBatch();
		List<String> chatIds = new ArrayList<>();
		targetGroupObjects.forEach(t -> chatIds.add(t.getString("chat_id")));
		// 发送消息
        chatIds.forEach(c -> {
			// 组装数据
			JSONObject bodyJson = new JSONObject();
			bodyJson.put("receive_id", c);
			bodyJson.put("msg_type", "text");
			HashMap<String, String> contentMap = new HashMap<>(2);
			String message = "根据elevator_num更新" + FeiShuDocumentEnum.getDescription(appToken,tableId) + "表格信息，结果：" + result;
			contentMap.put("text", message);
			// 将 contentMap 转换成 JSON 格式
			bodyJson.put("content", JSON.toJSONString(contentMap));
			feiShuDocumentService.commandSendGroupMessage(bodyJson.toJSONString());
		});
	}
}
